# This file is the master replication code 
# for Head, K. and T. Mayer, 2023, "Poor Substitutes? Counterfactual methods in IO and Trade compared", Review  of Economics and Statistics
 #
 #1) BLPdata part (Tables 1 to 3, Figures 1 and 2)
 source("Rfiles/CF_BLPdata.R")  #  runs CF for BLP data with original sd log income parameter
 source("Rfiles/CF_BLPdata_ysdloop.R") #  runs CF for BLP data with different sd for log income
 # This is the master file for this part:
 # Does all the computations of BLP moments + runs CF for BLP data + looks at rich subs with original BLP data simulated.
 
 #2) RC on domestic (Table 4, Figure 3)
 source("domestic_chars.R") # compute characteristics differences for domestic vs foreign 
 # BLP estimation including domestic mean and s.d. coefficients is done using pyblp package by CG2020 
 # open terminal (in /Replication): 
 system("./run_pyblp.sh") # takes 4 hours
 source("read_pyblp_output.R") # compiles results to run CF and output appendix table
 source("Rfiles/CF_BLPdata_domestic.R") #  runs CF for BLP data with domestic RC
 source("Rfiles/CF_BLPdata_domestic_SD_parallel.R") #  runs CF for BLP data with domestic RC, loops over sigma domestic
 source("Rfiles/SDdomestic_plot.R") # generates Figure 3
 
 #3) Dissection part with simulations calibrated on BLP data moments (Tables 6 to 8, Figures 4 and 5)
 source("Rfiles/CF_MMX_calibration.R") # should be run to match all the moments in the fastest way possible. Also creates the settings to be used in the actual simulations 
 source("Rfiles/CF_MMX_ABC.R")  # A: mlog-mc  B: mlog-oly  C: mces-oly
 source("Rfiles/CF_MMX_ABC_aggeta.R")  # This is the version that adds "macro" estimates of eta
 source("Rfiles/slides_tables_figures.R") # uses RDS files generated by CF_MMX_ABC.R
 
 
 #4) Robustness tests
 source("Rfiles/CF_BLPdata_uniqueness.R") #  runs CF for BLP data with different dampening parameters
 
 